<?php

/**

 * 分销设置逻辑层

 * @author maniac

 */

namespace app\common\logic;


use app\common\model\Options AS OptionsModel;

use app\facade\UserLogs;


class Distribution extends Options

{    

    /**

     * 编辑

     * @param  array $data 请求的信息

     */

    public function save($data)

    {

        $db = $data['db'];

        if($db['commission_level'] == 1){ //判断是否为一级分销
            if(empty($db['commission_charge_first'])){
                exception('一级佣金不能为空');
            }
        }
        if($db['commission_level'] == 2){ //判断是否为二级分销
            if(empty($db['commission_charge_first'])){
                exception('一级佣金不能为空');
            }
            if(empty($db['commission_charge_second'])){
                exception('二级佣金不能为空');
            }
        }

        if($db['commission_become'] == 2){ //判断成为分销商条件
            if(empty($db['commission_become_order_count'])){
                exception('订单次数不能为空');
            }
        }

        if($db['commission_become'] == 3){ //判断成为分销商条件
            if(empty($db['commission_become_money_count'])){
                exception('消费金额不能为空');
            }

        }

        if($db['commission_become'] == 4){ //判断成为分销商条件
            if(empty($db['commission_become_goods_id'])){
                exception('购买商品不能为空');
            }
        }


        $update = [

            'id'           => $data['id'],

            'option_name'  => $data['option_name'],

            'option_value' => serialize($db),

        ];

        OptionsModel::update($update);

        UserLogs::insertLog(['title' => '修改分销设置', 'content' => "id={$data['id']}\r\noption_name={$data['option_name']}"]);

    }

}

